package com.ynet.user.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 此工具类 对时间的操作
 *
 * @author liwq
 */
public class DateUtil {

    /**
     * @param format 日期格式 默认 yy-MM-dd
     * @param date   日期
     * @return String
     */
    public static String formatDate(String format, Date date) {
        if (date == null) {
            return "";
        }
        if (format == null || "".equals(format)) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 重载
     *
     * @param date 日期格式 默认 yy-MM-dd
     * @param date 日期
     * @return String
     */
    public static String formatDate(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    /**
     * 儒略历转成日期 date
     *
     * @param date
     * @return 返回date日期类型
     */
    public static Date juLianToDate(int date) {
        int year = (date / 1000) + 1900;
        int dayOfYear = date % 1000;

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);

        return calendar.getTime();
    }

    /**
     * 儒略历转成日期字符串
     *
     * @param date
     * @return
     */
    public static String juLianFormatDate(Integer date) {
        if (date == null) {
            return "";
        }
        Date dateJ = juLianToDate(date);
        return formatDate(dateJ);
    }

    /**
     * data日期类型 转换为儒略历格式
     *
     * @param date
     * @return int
     */
    public static int dateToJuLian(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR) - 1900;
        int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        return year * 1000 + dayOfYear;
    }

    /**
     * 日期字符串类型（yyyy-MM-dd） 转换为儒略历格式
     *
     * @param str
     * @return int
     */
    public static int dateStrToJuLian(String str) {
        if ("".equals(str) || str == null)
            return 0;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        int time = 0;
        try {
            date = simpleDateFormat.parse(str);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int year = calendar.get(Calendar.YEAR) - 1900;
            int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
            time = year * 1000 + dayOfYear;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 获取当前时间的时间戳 返回是年月日时分秒 例如20161026101049
     *
     * @return
     */
    public static String getTimeStamp() {
        Calendar calendar = Calendar.getInstance();
        Date nowDate = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyyMMddHHmmss");
        return simpleDateFormat.format(nowDate);
    }

    /**
     * 字符串转成日期 date
     *
     * @param dateStr
     * @return 返回date日期类型
     */
    public static Date dateStrToTimeStamp(String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyyMMddHHmmss");
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    public static void main(String[] args) {
//		String now = DateUtil.getTimeStamp();
//		System.out.println(now);
//		Date d1 = DateUtil.dateStrToTimeStamp("20190111142016");
//		Date d2 = DateUtil.dateStrToTimeStamp("20190111142320");
//		System.out.println(d2);
//		long diff = ((d2.getTime()-d1.getTime())/1000);
//		System.out.println(diff);
//		if(diff>(3*60)){//时间差大于3min,则返回错误
//			System.out.println("时间戳超时");
//		}else{
//			System.out.println("未超时");
//		}


    }

}
